package com.jd.cmdb.tsc.processor;

import com.jd.cmdb.tsc.entity.Task;
import com.jd.cmdb.tsc.jmq.JMQService;
import com.jd.cmdb.tsc.service.TaskService;
import com.jd.cmdb.tsc.util.CronTabUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * Created by tianzhengguo on 2016/1/11.
 */
@Service("processor")
public class DefaultProcessor implements IProcessor {

    private static final Logger logger = LoggerFactory.getLogger(DefaultProcessor.class);

    private static final SimpleDateFormat SDF = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    @Resource
    private TaskService taskService;

    @Resource
    private JMQService jmqService;

    @Override
    public List<Task> selTasks() throws Exception{
        return taskService.selAllTasks();
    }

    @Override
    public void process(Task task){
        logger.debug("cron_tab:{}", task.getCronTab());
        logger.debug("task:{}", task.toString());

        try{
            short state = task.getState();
            boolean mustSend = false;

            switch (state){
                case TaskService.STATE_READY:
                case TaskService.STATE_SUCCESS:{
                    mustSend = mustCompareTimeTask(task);
                    break ;
                }
                case TaskService.STATE_WORKER_INNER_FAIL: {
                    mustSend = true;
                    Timestamp nextTime = new Timestamp(CronTabUtil.getNextDate(task.getCronTab(), new Date()).getTime());
                    task.setNextStartTime(nextTime);
                    break ;
                }
                case TaskService.STATE_WORKER_OUTER_FAIL: {
                    break ;
                }
                case TaskService.STATE_RUNNING: {
//                    if()
                    break ;
                }
                default: break;
            }

            if(mustSend){
                logger.info("发送任务:{}.{},id:{},state:{}", task.getName(), task.getSubName(), task.getId(), task.getState());
                jmqService.send(task);
                taskService.updateTask(task);
            }
        }catch(Exception e){
//            e.printStackTrace();
            logger.error("处理过程出错:{}",e.getMessage());
        }
    }

    //需要进行时间比对的任务
    private boolean mustCompareTimeTask(Task task) throws Exception{
        Date curDate = new Date();
        Timestamp nextTime = task.getNextStartTime();
        long nextTimeL = nextTime.getTime();

        Date nextDate = CronTabUtil.getNextDate(task.getCronTab(), nextTime);

        Timestamp curTime = Timestamp.valueOf(SDF.format(curDate));
        long curTimeL = curTime.getTime();
        logger.info("taskName:{},nextTime:{},curTime:{}", task.getName(), nextTime, curTime);
        if(nextTimeL <= curTimeL) {
            if(nextTimeL < curTimeL)
                task.setNextStartTime(new Timestamp(CronTabUtil.getNextDate(task.getCronTab(), curTime).getTime()));
            else
                task.setNextStartTime(Timestamp.valueOf(SDF.format(nextDate)));
//            task.setState(TaskService.STATE_RUNNING);
            return true;
        }
        return false;
    }
}
